<template>
	<xn-form-container
		:title="formData.id ? '编辑' : '新增'"
		width="75%"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="仓库名称" name="warehouseId">
						<a-select :getPopupContainer="trigger => trigger.parentNode" v-model:value="formData.warehouseId" placeholder="请选择仓库">
							<a-select-option v-for="item in warehouseinfoList" :key="item.value" :value="item.value">{{
								item.label
							}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="商品名称" name="materialId">
						<a-select :getPopupContainer="trigger => trigger.parentNode" v-model:value="formData.materialId" placeholder="请选商品名称">
							<a-select-option v-for="item in promaterialList" :key="item.value" :value="item.value">{{
								item.label
							}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="含税单价" name="waresPrice">
						<a-input v-model:value="formData.waresPrice" placeholder="请输入含税单价" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="库存数量" name="waresNum">
						<a-input v-model:value="formData.waresNum" placeholder="请输入库存数量" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">提交</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="productForm">
	import { required } from '@/utils/formRules'
	import warehouseApi from '@/api/warehouse/warehouseApi'
	import promaterialApi from '@/api/base_data/promaterialApi'
	import storeCenterApi from '@/api/base_data/storeCenterApi'
	// 定义emit事件
	//PROCUREMENT_PARTNERS
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	const visible = ref(false)
	const warehouseinfoList = ref([])
	const promaterialList = ref([])
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		initData()
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {}
		}
	}
	const initData = () => {
		getWarehouseinfos()
		getPromaterialPage()
	}

	//仓库列表
	const getWarehouseinfos = () => {
		storeCenterApi
			.getPage({})
			.then((res) => {
				warehouseinfoList.value = res.records.map((element) => {
					return {
						label: element.warehouseName,
						value: element.id
					}
				})
			})
			.catch(() => {})
	}
	const getPromaterialPage = () => {
		promaterialApi
			.getPage({})
			.then((res) => {
				promaterialList.value = res.records.map((element) => {
					return {
						label: element.name,
						value: element.id
					}
				})
			})
			.catch(() => {})
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		warehouseId: [required('请选择仓库')],
		materialId: [required('请选择商品')],
		waresNum: [
			required('请输入库存数量'),
			{
				message: '请输入数字',
				pattern: /^[0-9]*$/,
				required: true,
				trigger: 'change'
			}
		],
		waresPrice: [
			required('请输入含税单价'),
			{
				message: '请输入合法的金额数字，最多两位小数',
				pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
				required: true,
				trigger: 'change'
			}
		]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				let params = Object.assign(formData.value)
				params.waresAmount = Number((params.waresPrice * params.waresNum).toFixed(2))
				warehouseApi
					.addWarehouse(params)
					.then(() => {
						visible.value = false
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped :type="less">
	.childAddButton {
		margin-bottom: 10px;
	}
</style>
